package cn.cit.wcif.common.utils;

/**
 *  DateUtil.java 2011-1-18 
 *
 *  Copyright (c) 2010-2012 Cyber Business Information Tech Co.,Ltd. 
 *  All rights reserved.
 *
 *    www.citchina.cn
 *    rd@citchina.cn
 */
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * The is a Util for process Date.
 * 
 * @author Jonathan Lai
 * @version 0.1
 * @since 0.1
 */
public class DateUtil {

	public static final String yyMMdd = "yy-MM-dd";
	public static final String yyyyMMdd = "yyyy-MM-dd";
	public static final String HHmmss = "HH:mm:ss";
	public static final String HHmm = "HH:mm";
	public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
	public static final String yyMMddHHmmss = "yy-MM-dd HH:mm:ss";

	/**
	 * 
	 * @param s
	 * @param style
	 * @return
	 */
	public static Date parseToDate(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		if (s == null || s.length() < 8)
			return null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 
	 * @param s
	 * @param style
	 * @return
	 */
	public static java.sql.Date parseToSQLDate(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		java.sql.Date date = null;
		if (s == null || s.length() < 8)
			return null;
		Date dd = DateUtil.parseToDate(s, style);
		date = new java.sql.Date(dd.getTime());
		return date;
	}

	/**
	 * 
	 * @param dat
	 * @param style
	 * @return
	 */
	public static java.sql.Date parseToSQLDate(java.util.Date dat, String style) {

		java.sql.Date date = null;
		if (dat == null || dat.toString().length() < 8)
			return null;
		date = new java.sql.Date(dat.getTime());
		return date;
	}

	/**
	 * 
	 * @param s
	 * @param style
	 * @return
	 */
	public static String parseToString(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		String str = null;
		if (s == null || s.length() < 8)
			return null;
		try {
			date = simpleDateFormat.parse(s);
			str = simpleDateFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 
	 * @param date
	 * @param style
	 * @return
	 */
	public static String parseToString(Date date, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		String str = null;
		if (date == null)
			return null;
		str = simpleDateFormat.format(date);
		return str;
	}

	/**
	 * 
	 * @param date
	 * @param style
	 * @return
	 */
	public static String parseSQLDateToString(java.sql.Date date, String style) {
		if (date == null)
			return null;
		java.util.Date d = new java.util.Date(date.getTime());
		return parseToString(d, style);
	}

	/**
	 * Getting java.sql.Date on this time.
	 * 
	 * @return
	 */
	public static java.sql.Date getJavaSqlDate() {
		java.sql.Date date = null;
		Date dd = new Date();
		date = new java.sql.Date(dd.getTime());
		return date;
	}

	/**
	 * 
	 * @param timestamp
	 * @param style
	 * @return
	 */
	public static String parseTimestampToString(java.sql.Timestamp timestamp,
			String style) {
		if (timestamp == null)
			return null;
		return DateUtil.parseToString(timestamp.toString(),
				DateUtil.yyyyMMddHHmmss);
	}

	/**
	 * 
	 * @return
	 */
	public static String getTimestampDate() {
		Timestamp ts = new Timestamp(new Date().getTime());
		return DateUtil.parseToString(ts.toString(), DateUtil.yyyyMMddHHmmss);
	}

}
